//注册页面
routeApp.controller('register', ["$scope", "$rootScope", "$http", "$interval","$location", function (scope, rootScope, http,time,location) {
    
    //用户信息，默认全部为空
    scope.yonghu = {};

    scope.sendcode = function () {
        //代表还存在数据
        if ($(".sendcode").attr("disabled") == true) {
            return;
        }

        //先验证手机号
        var sjh = scope.yonghu.shoujihao;
        if (sjh == "") {
            alert("请填写手机号!");
            return;
        }

        //成功后执行
        http.get("/reg/regsendcode?shoujihao="+sjh).success(function (response) {
            if (response.success) {//成功
                alert(response.msg);
                
                var i = 300;
                var t = time(function () {                    
                    if (i < 0) {
                        $(".sendcode").removeClass("sendcode_ok");
                        $(".sendcode").html("获取手机验证码")
                        $(".sendcode").removeAttr("disabled");
                        if (angular.isDefined(t)) t.cancel();
                    } else {
                        $(".sendcode").addClass("sendcode_ok");
                        $(".sendcode").html(i + "秒后重新获取");
                        $(".sendcode").attr("disabled", "true");
                        i--;
                    }
                }, 1000);
            } else {//失败
                alert(response.msg);
                $(".sendcode").attr("disabled", false);
                $(".sendcode").removeAttr("disabled");
            }
        });
    }

    //注册用户信息
    scope.regsave = function () {
        var user = scope.yonghu;
        //判断用户信息,这里需要严格的判断，手机号码，密码的
        if (user.shoujihao == "") {
            alert("请填写手机号!");
            return;
        }

        if (user.mima == "") {
            alert("请填写密码!");
            return;
        }

        if (user.yanzhengma == "") {
            alert("请填写验证码!");
            return;
        }

        http.post("/reg/regsubmit", user).success(function (response) {
            if (response.success && response.msg == "OK") {//成功
                alert("注册成功!");
                location.path("");
            } else {//失败
                alert(response.msg);              
            }
        });
    }

}]);

//用户登录页面
routeApp.controller('login', ["$scope", "$rootScope", "$http", "$interval", "$location", function (scope, rootScope, http, time, location) {
    //用户信息，默认全部为空
    scope.yonghu = {};

    //注册用户信息
    scope.checklogin = function () {
        var user = scope.yonghu;
        //判断用户信息,这里需要严格的判断，手机号码，密码的
        if (user.shoujihao == "") {
            alert("请填写手机号!");
            return;
        }

        if (user.mima == "") {
            alert("请填写密码!");
            return;
        }

        http.post("/reg/checklogin", user).success(function (response) {
            if (response.success && response.msg == "OK") {//成功
                alert("登录成功!");
                location.path("");
            } else {//失败
                alert(response.msg);
            }
        });
    }

}]);